package com.richapp.launcher.ui.fragments.child.light;

import java.util.List;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ListView;

import com.richapp.launcher.R;
import com.richapp.launcher.database.system.dao.RoomModelDao;
import com.richapp.launcher.database.system.mode.RoomModel;
import com.richapp.launcher.ui.adapter.ChildRoomLightGridAdapter;
import com.richapp.launcher.ui.adapter.ChildRoomLightListAdapter;
import com.richapp.launcher.ui.fragments.BaseChildFragment;

public class LightControlFragment extends BaseChildFragment{
	private ListView roomLightList = null;
	private GridView roomLightGrid = null;
	private FrameLayout light_child_arrow_up = null;
	private ChildRoomLightListAdapter childRoomLightListAdapter = null;
	private ChildRoomLightGridAdapter chiRoomLightGridAdapter = null;
	private List<RoomModel> roomList = null;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.light_control, container, false);
		init(view);
		roomList = new RoomModelDao(getActivity().getApplicationContext()).getRoomList();
		return view;
	}

	protected void init(View view) {
		light_child_arrow_up = (FrameLayout) view.findViewById(R.id.light_child_arrow_up);
		light_child_arrow_up.setNextFocusDownId(R.id.child_roomlight_list);
		// 灯光控制的ListView
		roomLightList = (ListView) view.findViewById(R.id.child_roomlight_list);
		childRoomLightListAdapter = new ChildRoomLightListAdapter(getActivity(), roomList);
		roomLightList.setAdapter(childRoomLightListAdapter);
		roomLightList.setOnItemClickListener(itemClick);

		// 灯光控制的GridView
		roomLightGrid = (GridView) view.findViewById(R.id.child_roomlight_gridview);
		roomLightGrid.setOnItemClickListener(itemClick);
	}

	boolean flag = false;
	OnItemClickListener itemClick = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
			switch (parent.getId()) {
			case R.id.child_roomlight_list:
//				List<Device> deviceList = room.getDeviceList();
//				chiRoomLightGridAdapter.setDeviceList(deviceList);
//				chiRoomLightGridAdapter.notifyDataSetChanged();
				break;
			case R.id.child_roomlight_gridview:
//				Device childNode = (Device)parent.getAdapter().getItem(position);
//				flag = !flag;
//				ImageView lamp_switch = (ImageView) view.findViewById(R.id.lamp_switch);
//				lamp_switch.setTag(flag);
//				if ((Boolean) (lamp_switch.getTag())) {
//					lamp_switch.setImageResource(R.drawable.child_light_on);
//				} else {
//					lamp_switch.setImageResource(R.drawable.child_light_off);
//				}
				break;
			default:
				break;
			}
		}
	};

	public static LightControlFragment getInstance() {
		return new LightControlFragment();
	}
}
